package com.jianggy.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author jianggy
 * @version 1.0
 * @time 2019/11/29 16:00
 * @see
 **/
public class App1 {
    public static void main(String[] args){
        String url = "jdbc:mysql://localhost:3306/test";
        String driverClass = "com.mysql.jdbc.Driver";
        String username = "root";
        String psw = "1qaz!QAZ";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        try {
            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(url,username,psw);
            connection.setAutoCommit(false);
            connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            Statement statement = connection.createStatement();
            ResultSet resultSet =  statement.executeQuery("SELECT * FROM test where id=1  ");
            resultSet.next();
            int height = resultSet.getInt("height");
            System.out.println("App1:"+height);
            long version = resultSet.getLong("version");
            // TimeUnit.SECONDS.sleep(5);
            String sql = "update test set height= "+(height+3)+",version="+System.currentTimeMillis()+" where id=1 and version="+version;
            int res = statement.executeUpdate(sql);
            connection.commit();
            System.out.println(new Date());
            if (res==1){
                System.out.println("App1提交成功,修改为"+(height+3));
            }else{
                System.out.println("App1修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
